package com.jic.veteran.portal.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.mapper.InvoiceDetailMapper;
import com.jic.veteran.mapper.InvoiceInfoMapper;
import com.jic.veteran.mapper.ProjectDonationDetailMapper;
import com.jic.veteran.model.InvoiceDetail;
import com.jic.veteran.model.InvoiceInfo;
import com.jic.veteran.model.ProjectDonationDetail;
import com.jic.veteran.model.ProjectDonationDetailExample;
import com.jic.veteran.portal.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceInfoMapper invoiceInfoMapper;
    @Autowired
    private InvoiceDetailMapper invoiceDetailMapper;
    @Autowired
    private ProjectDonationDetailMapper projectDonationDetailMapper;

    @Override
    public int addInvoiceInfo(InvoiceInfo invoiceInfo) {
        if (StrUtil.isBlank(invoiceInfo.getOpenId()) || StrUtil.isBlank(invoiceInfo.getInvoiceType())) {
            return 0;
        }
        List<InvoiceInfo> invoiceInfoList = invoiceInfoMapper.getInvoiceInfoByOpenId(invoiceInfo.getOpenId(), invoiceInfo.getInvoiceType());
        if (ObjectUtil.isNotEmpty(invoiceInfoList)) {
            return invoiceInfoMapper.update(invoiceInfo,invoiceInfoList.get(0).getId());
        }
        return invoiceInfoMapper.insert(invoiceInfo);
    }

    @Override
    public List<InvoiceInfo> getInvoiceInfo(String openId, String invoiceType) {
        return invoiceInfoMapper.getInvoiceInfoByOpenId(openId, invoiceType);
    }

    @Override
    public int addInvoiceDetail(InvoiceDetail invoiceDetail) {
        return invoiceDetailMapper.insert(invoiceDetail);
    }

    @Override
    public int batchAddInvoiceDetail(List<String> ids, String mailAddress, String openId, String invoiceType) {
        int result = 0;
        if (!ids.isEmpty()) {
            for (int i = 0; i < ids.size(); i++) {
                ProjectDonationDetailExample detailExample = new ProjectDonationDetailExample();
                detailExample.createCriteria().andReqsnEqualTo(ids.get(i));
                List<ProjectDonationDetail> projectDonationDetails = projectDonationDetailMapper.selectByExample(detailExample);
                if (!projectDonationDetails.isEmpty()) {
                    ProjectDonationDetail projectDonationDetail = projectDonationDetails.get(0);
                    InvoiceDetail invoiceDetail = new InvoiceDetail();
                    invoiceDetail.setOpenId(projectDonationDetail.getUserId());
                    invoiceDetail.setAmount(projectDonationDetail.getDonationAmount());
                    invoiceDetail.setProjectId(projectDonationDetail.getProjectId());
                    invoiceDetail.setProjectName(projectDonationDetail.getProjectName());
                    invoiceDetail.setMailAddress(mailAddress);
                    List<InvoiceInfo> invoiceInfoList = invoiceInfoMapper.getInvoiceInfoByOpenId(openId, invoiceType);
                    if (ObjectUtil.isEmpty(invoiceInfoList)) {
                        continue;
                    }
                    InvoiceInfo invoiceInfo = invoiceInfoList.get(0);
                    invoiceDetail.setInvoiceName(invoiceInfo.getInvoiceName());
                    invoiceDetail.setTaxNumber(invoiceInfo.getTaxNumber());
                    invoiceDetail.setInvoiceStatus("1");
                    invoiceDetail.setOrderId(ids.get(i));
                    //invoiceDetail.setInvoiceContent("");
                    invoiceDetail.setTaxMail(invoiceInfo.getTaxMail());
                    invoiceDetail.setInvoiceType(invoiceType);
                    invoiceDetail.setInvoiceTime(DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
                    result = this.addInvoiceDetail(invoiceDetail);
                    if (result < 1) {
                        return result;
                    }
                }
            }
        }
        return result;
    }

    @Override
    public List<InvoiceDetail> historyList(String openId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return invoiceDetailMapper.listHistory(openId);
    }

    @Override
    public List<ProjectDonationDetail> unInvoiceList(String openId) {
        return invoiceDetailMapper.unVoicelist(openId);
    }

    @Override
    public InvoiceDetail getItem(String reqsn) {
        return invoiceDetailMapper.getItem(reqsn);
    }
}
